/**
 * 
 */
package net.homeip.mmhg.security;

import net.homeip.mmhg.dao.TeamDao;
import net.homeip.mmhg.domain.Team;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

/**
 * Custom wrapper for <tt>UserDetailService</tt>,
 *  loads custom user details from data store.
 * 
 *  @author ilya gerasimov
 */
public class UserDetailServiceWrapper implements UserDetailsService {

	private TeamDao teamDao = null;

	@Autowired
	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	@Override
	public UserDetails loadUserByUsername(String name)
			throws UsernameNotFoundException, DataAccessException {
		Team team = teamDao.getTeam(name);
		if (team == null) {
			throw new UsernameNotFoundException(name);
		}
		return new Team2UserDetailsAdapter(team);
	}
	

}
